/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.generics.client;

import br.com.sales.generics.client.nametoken.NameTokens;
import br.com.sales.generics.client.presenter.MyMainLayoutPresenter;
import br.com.sales.generics.client.utils.User;
import com.google.gwt.core.client.GWT;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.gwtplatform.mvp.shared.proxy.PlaceRequest;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author v
 * @param <V>
 * @param <P>
 */
public abstract class HanAbstractPresenter<V extends View, P extends Proxy<?>> extends Presenter<V, P> {

    @Inject
    public PlaceManager placeManager;
    private boolean scrollPlace = true;
    private String token;
    @Inject
    private User user;
    private final Map<String, String> parameters = new HashMap<String, String>();

    public HanAbstractPresenter(EventBus eventBus, V view, P proxy) {
        super(eventBus, view, proxy);
    }

    @Override
    protected void revealInParent() {
        super.revealInParent();
        MyMainLayoutPresenter.SCROLL_PLACE = scrollPlace;

        if (needsAuthorization()) {
            revealAuthorizedPlace();
        } else {
            RevealContentEvent.fire(HanAbstractPresenter.this, MyMainLayoutPresenter.CONTENT_SLOT_TYPE, HanAbstractPresenter.this);
        }
    }

    private boolean needsAuthorization() {
        return !NameTokens.LOGIN.equals(token);
    }

    private void revealAuthorizedPlace() {
        GWT.log("Nome user: " + user.getNmUser());
        if (!"".equals(user.getNmUser())
                && user.getNmUser() != null) {
            RevealContentEvent.fire(HanAbstractPresenter.this, MyMainLayoutPresenter.CONTENT_SLOT_TYPE, HanAbstractPresenter.this);
        } else {
            revealPlace(NameTokens.LOGIN);
        }
    }

    @Override
    public void prepareFromRequest(PlaceRequest request) {
        this.token = removeParametersFromNameToken(request);
        parameters.clear();

        for (String param : request.getParameterNames()) {
            parameters.put(param, request.getParameter(param, null));
        }

        if (hasRedirection(parameters)) {
            doRedirect(parameters);
        }

        super.prepareFromRequest(request);
    }

    private String removeParametersFromNameToken(PlaceRequest request) {
        String nameToken = request.getNameToken();
        for (String param : request.getParameterNames()) {
            String regex = new StringBuilder("\\/\\{").append(param).append("}").toString();
            nameToken = nameToken.replaceAll(regex, "");
        }
        return nameToken;
    }

    private boolean hasRedirection(Map<String, String> parameters) {
        return parameters.containsKey("q");
    }

    private void doRedirect(Map<String, String> parameters) {
        revealPlace(parameters.remove("q"), parameters);
    }

    protected void setScrollPlace(boolean scrollPlace) {
        this.scrollPlace = scrollPlace;
    }

    protected void revealPlace(String nameToken) {
        placeManager.revealPlace(new PlaceRequest.Builder().nameToken(nameToken).build(), true);
    }

    protected void revealPlace(String nameToken, Map<String, String> parametros) {
        PlaceRequest.Builder builder = new PlaceRequest.Builder().nameToken(nameToken);
        for (String name : parametros.keySet()) {
            builder.with(name, parametros.get(name));
        }
        placeManager.revealPlace(builder.build(), true);
    }
}
